package com.hust360.percent.adapter;

import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.hust360.percent.PercentApplication;
import com.hust360.percent.R;
import com.hust360.percent.pojo.Area;
import com.hust360.percent.utils.PercentThreadPool;

public class AreaListAdapter extends BaseAdapter {

	private Context context;
	private PercentApplication application;
	private LayoutInflater inflater;
	private List<Area> list;

	/*
	 * 测试时候用的放图片的文件夹，后台搭建之后去除
	 */
	@SuppressLint("SdCardPath")
	String parentPath = "/sdcard/area_file";

	public AreaListAdapter(Context context, List<Area> list) {
		super();
		this.context = context;
		application = (PercentApplication) this.context.getApplicationContext();
		inflater = (LayoutInflater) context
				.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	@Override
	public int getCount() {
		return list == null ? 0 : list.size();
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public Object getItem(int position) {
		return list.get(position);
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = inflater.inflate(R.layout.area_list_item, null);
			holder = new ViewHolder();
			holder.img = (ImageView) convertView
					.findViewById(R.area_list_item.image);
			holder.text = (TextView) convertView
					.findViewById(R.area_list_item.text);
			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
			holder.img.setImageResource(R.drawable.item_img_test);
		}
		Area area = list.get(position);
		holder.text.setText(area.getTitle());
		String img = area.getImg();
		if (img != null) {
			String string = position + "hehe";
			holder.img.setTag(string);
			application.getThreadPool().loadDrawable("", parentPath + "//" + img , new LoadImgCallback(convertView, string));
		}
		return convertView;
	}

	public List<Area> getList() {
		return list;
	}

	public void setList(List<Area> list) {
		this.list = list;
	}

	private class ViewHolder {
		ImageView img;
		TextView text;
	}

	/**
	 * 多线程加载图片的回调方法
	 */
	private class LoadImgCallback implements
			PercentThreadPool.DoneLoadingCallback {
		private View convertView;
		private Object imgTag;

		public LoadImgCallback(View convertView, Object imgTag) {
			this.convertView = convertView;
			this.imgTag = imgTag;
		}

		@Override
		public void doneLoading(Drawable drawable) {

			if (drawable == null) {
				return;
			}

			synchronized (convertView) {
				ImageView imgView = (ImageView) convertView
						.findViewWithTag(imgTag);
				if (imgView != null) {
					drawable.setBounds(0, 0, imgView.getWidth(), imgView.getHeight());
					imgView.setImageDrawable(drawable);
					imgView.invalidate();
				}
			}
		}
	}
}
